#! /usr/bin/env python3
# -----------------------------------------------------------------------------
# Copyright 2024 Bernd Pfrommer <bernd.pfrommer@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import subprocess

import rclpy


def main(args=None):
    rclpy.init(args=args)
    cname = '/metavision_driver_container'
    output = subprocess.run(['ros2', 'component', 'list', cname], stdout=subprocess.PIPE)
    lines = output.stdout.decode('utf-8').splitlines()
    stopped = False
    for line in lines:
        a = line.split()
        if a[1] == '/recorder':
            output = subprocess.run(
                ['ros2', 'component', 'unload', cname, f'{a[0]}'], stdout=subprocess.PIPE
            )
            stopped = True
    if stopped:
        print('recording stopped!')
    else:
        print('WARNING: recording not stopped because none was running!')


if __name__ == '__main__':
    main()
